﻿using Global.Tool;
using UnityEngine;
using DG.Tweening;
namespace Global.Manager
{
    /// <summary>
    /// 输入管理
    /// </summary>
    public class InputManager : UnitySceneSingleton<InputManager>, ImessageObject
    {
        private static readonly IGameInput GameInput = PlatformUtility.Input;
        private float _xSpeed = 1f;
        private float _ySpeed = 1f;
        private float _x = 0.0f;
        private float _y = 0.0f;
        private float _deltaX = 0;
        private float _deltaY = 0;
        private Vector3 _lastPosition = Vector3.zero;
        private float _lastScale;
        private float _lastMoveSpeedX = 0.0f;
        private float _lastMoveSpeedY = 0.0f;
        private float _ax = 0.0f;
        private float _ay = 0.0f;
        private bool _isTwing = false;
        #region 初始化Manager
        public void Init()
        {

        }
        #endregion
        #region 镜头运动函数
        /// <summary>
        /// 开始移动镜头
        /// </summary>
        /// <param name="isTouchDevice"></param>
        private void BeginMoveCamera()
        {
            if (GameInput.IsTouchDevice)
            {
                _lastPosition = GameInput.MousePosition;
            }
            else
            {
                _ax = (Mathf.Abs(WinGameInput.CameraMoveSpeed) - 0.0f) / 1.0f;
                _ay = (Mathf.Abs(WinGameInput.CameraMoveSpeed) - 0.0f) / 1.0f;
                _isTwing = true;
            }
        }
        /// <summary>
        /// 移动镜头
        /// </summary>
        private void MoveingCamera()
        {
            if (!GameInput.IsTouchDevice)
            {
                WinDeviceMoveingCamera();
            }
            else
            {
                if (!_isTwing)
                    TouchDeviceMoveingCamera();
            }
        }
        /// <summary>
        /// Windows设备移动镜头
        /// </summary>
        private void WinDeviceMoveingCamera()
        {
            _deltaX = -(GameInput.MousePosition - _lastPosition).x;
            _deltaY = -(GameInput.MousePosition - _lastPosition).y;
            var realDeltaX = (Mathf.Abs(_deltaX) > 9.99999944E-11f)? _deltaX:0;
            var realDeltaY = (Mathf.Abs(_deltaY) > 9.99999944E-11f)? _deltaY:0;
            var newPosX = Camera.main.transform.position.x + realDeltaX*_xSpeed*0.02f;
            var newPosY = Camera.main.transform.position.y + realDeltaY*_ySpeed*0.02f;
            var newPosZ = Camera.main.transform.position.z;
            var newPosition = new Vector3(newPosX,newPosY, newPosZ);
            if (!(newPosition.x > WinGameInput.EdgeRightX|| newPosition.x < WinGameInput.EdgeLeftX
                || newPosition.y < WinGameInput.EdgeDownY|| newPosition.y > WinGameInput.EdgeUpY))
            {
                Camera.main.transform.position = newPosition;
            }
            _lastPosition = GameInput.MousePosition;
        }
        /// <summary>
        /// Touch设备移动镜头
        /// </summary>
        private void TouchDeviceMoveingCamera()
        {
            var CameraMoveSpeed = WinGameInput.CameraMoveSpeed;
           //如果X的移动量大于Y 移动X
            if (Mathf.Abs(Input.GetTouch(0).deltaPosition.x) > Mathf.Abs(Input.GetTouch(0).deltaPosition.y))// X的移动量比Y的移动量大 防抖动用
            {
                
                _lastMoveSpeedY = 0.0f;
                float deltaX = Mathf.Abs(Input.GetTouch(0).deltaPosition.x * 0.2f);
                //边界判定
                if (Input.GetTouch(0).deltaPosition.x < 0 &&Camera.main.transform.localPosition.x < WinGameInput.EdgeRightX)
                {
                    _lastMoveSpeedX = CameraMoveSpeed * deltaX;
                    Camera.main.transform.Translate(_lastMoveSpeedX, 0, 0);
                }
                else if(Camera.main.transform.localPosition.x>WinGameInput.EdgeLeftX)
                {
                    _lastMoveSpeedX = -CameraMoveSpeed * deltaX;
                    Camera.main.transform.Translate(_lastMoveSpeedX, 0, 0);
                }
            }
            else
            {
                _lastMoveSpeedX = 0.0f;
                float deltaY = Mathf.Abs(Input.GetTouch(0).deltaPosition.y * 0.2f);
                if (Input.GetTouch(0).deltaPosition.y < 0 &&
                    Camera.main.transform.localPosition.y > WinGameInput.EdgeDownY)
                {
                    _lastMoveSpeedY = -CameraMoveSpeed * deltaY;
                    Camera.main.transform.Translate(_lastMoveSpeedY, 0, 0);
                }
                else if (Camera.main.transform.localPosition.y < WinGameInput.EdgeUpY)
                {
                    _lastMoveSpeedY = CameraMoveSpeed * deltaY;
                    Camera.main.transform.Translate(_lastMoveSpeedY, 0, 0);
                }
            }
        }
        /// <summary>
        /// 缩放镜头
        /// </summary>
        private void ZoomCamera()
        {
            if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                var curDist = Input.GetTouch(0).position - Input.GetTouch(1).position;
                var dist1 = (Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition);
                var dist2 = (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition);
                var preDist = dist1 - dist2;
                var touchDelta = curDist.magnitude - preDist.magnitude;
                if (touchDelta > 0)
                {
                    if (Camera.main.orthographicSize - 0.5f > 5.0f && Camera.main.orthographicSize - 0.5f < 12.0f)
                        Camera.main.orthographicSize += 0.5f;
                }
                if (touchDelta < 0)
                {
                    if (Camera.main.orthographicSize - 0.5f > 5.0f && Camera.main.orthographicSize - 0.5f < 12.0f)
                        Camera.main.orthographicSize -= 0.5f;
                }
            }
        }

        private void LateCameraMove()
        {
           
            if (Mathf.Abs(_lastMoveSpeedX) - 0.0f < WinGameInput.EdgeWindth)
            {
                _lastMoveSpeedX = 0.0f;
                _ax = 0.0f;
                _isTwing = false;
            }
            if (Mathf.Abs(_lastMoveSpeedY) - 0.0f < WinGameInput.EdgeWindth)
            {
                _lastMoveSpeedY = 0.0f;
                _ay = 0.0f;
                _isTwing = false;
            }
            if (_ax != 0.0f)
            {
                if (_lastMoveSpeedX < 0)
                    _lastMoveSpeedX += _ax*Time.deltaTime;
                else
                    _lastMoveSpeedX -= _ax*Time.deltaTime;
                if (Camera.main.transform.localPosition.x > WinGameInput.EdgeLeftX
                    && Camera.main.transform.localPosition.x < WinGameInput.EdgeRightX)
                {
                    Camera.main.transform.Translate(_lastMoveSpeedX, 0, 0);
                }
                else
                {
                    _lastMoveSpeedX = 0.0f;
                    _isTwing = false;
                }
            }
            else if (_ay != 0.0f)
            {
                if (_lastMoveSpeedY < 0)
                    _lastMoveSpeedY += _ay * Time.deltaTime;
                else
                    _lastMoveSpeedY -= _ay * Time.deltaTime;
                if (Camera.main.transform.localPosition.y > WinGameInput.EdgeDownY
                    && Camera.main.transform.localPosition.y < WinGameInput.EdgeUpY)
                {
                    Camera.main.transform.Translate(_lastMoveSpeedY, 0, 0);
                }
                else
                {
                    _lastMoveSpeedY = 0.0f;
                    _isTwing = false;
                }
            }
        }
        #endregion
        public void LateUpdate()
        {
            this.START_METHOD("LateUpdate");
            // 开始移动判定
            if (GameInput.IsClickDown) BeginMoveCamera();
            // 惯性
            LateCameraMove();
            // 移动镜头
            if (GameInput.IsMove) MoveingCamera();
            // 缩放镜头
            if (GameInput.TouchCount > 1) ZoomCamera();
            this.END_METHOD("LateUpdate");
        }

    }
}
